/*
 * Date：10/23/18 10:08 PM
 * Author: guomw
 *
 */

package org.elanfox.intellicommunity.biz.repository.equipment;

import org.elanfox.intellicommunity.biz.entity.equipment.LocatorAreaPO;
import org.elanfox.intellicommunity.biz.entity.equipment.LocatorPO;
import org.elanfox.intellicommunity.data.base.base.JpaSearchableRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 定位器Repository
 *
 * @author loutianxi
 * @date 2019-12-04
 */
@Repository
public interface LocatorRepository extends JpaSearchableRepository<LocatorPO, Integer> {

    /**
     * 更新启用状态
     *
     * @param locationId
     * @param enable
     * @return
     */
    @Query("update LocatorPO c set c.enable=?2 where c.locatorId=?1")
    @Modifying(clearAutomatically = true)
    @Transactional(rollbackFor = RuntimeException.class)
    int updateEnableById(Integer locationId, boolean enable);

    /**
     * 更新启用状态
     *
     * @param no
     * @param enable
     * @return
     */
    @Query("update LocatorPO c set c.enable=?2 where c.no=?1")
    @Modifying(clearAutomatically = true)
    @Transactional(rollbackFor = RuntimeException.class)
    int updateEnableByNo(Long no, boolean enable);

    /**
     * 删除定位器
     *
     * @param no
     * @return
     */
    @Query("delete from LocatorPO c  where c.no=?1")
    @Modifying(clearAutomatically = true)
    @Transactional(rollbackFor = RuntimeException.class)
    int deleteByNo(Long no);

    /**
     * 更新防拆卸状态
     *
     * @param locatorId
     * @param dismantleStatus
     * @return
     */
    @Query("update LocatorPO c set c.dismantleStatus=?2 where c.locatorId=?1")
    @Modifying(clearAutomatically = true)
    @Transactional(rollbackFor = RuntimeException.class)
    int updateDismantleStatusById(Integer locatorId, boolean dismantleStatus);

    /**
     * 根据楼层id获取定位器列表
     *
     * @param structureId
     * @return
     */
    List<LocatorPO> findByLocatorInfo_StructureId(Integer structureId);

    /**
     * 根据楼层id获取需要在地图上显示的定位器列表
     *
     * @param structureId
     * @return
     */
    List<LocatorPO> findByLocatorInfo_StructureIdAndShowIsTrue(Integer structureId);

    /**
     * 更新展示状态
     *
     * @param locationId
     * @param enable
     * @return
     */
    @Query("update LocatorPO c set c.show=?2 where c.locatorId=?1")
    @Modifying(clearAutomatically = true)
    @Transactional(rollbackFor = RuntimeException.class)
    int updateShow(Integer locationId, boolean enable);

    /**
     * 根据no获取定位器
     *
     * @param no
     * @return
     */
    LocatorPO findByNo(Long no);

    @Query("select c from LocatorPO c where c.enable=1  order by c.createTime")
    List<LocatorPO> findLocator();


    long countAllByEnableIsTrue();

    @Query(nativeQuery = true, value =
            "select a.* from t_locator a left join t_structure b on a.structure_id=b.structure_id where b.p_structure_id=:pStructureId and b.structure_id !=:structureId")
    List<LocatorPO> findLocatorpoByPStrId(@Param("pStructureId") Integer pStructureId, @Param("structureId") Integer structureId);

    @Query("update LocatorPO po set po.areaPO=?1 where po.locatorId in ?2")
    @Modifying
    @Transactional
    int updateArea(LocatorAreaPO po, List<Integer> ids);

    List<LocatorPO> findByAreaPO_Id(Integer areaId);
}
